package com.kungfu.service.impl;

import com.kungfu.config.Constant;
import com.kungfu.dao.*;
import com.kungfu.entity.CyxjInfoEntity;
import com.kungfu.entity.OutHospitalInfo;
import com.kungfu.entity.YpcdEntity;
import com.kungfu.entity.request.BaseDataEntity;
import com.kungfu.service.MxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MxServiceImpl implements MxService {
    @Autowired
    private HkXhCommonRuleMapper commonMapper;

    @Autowired
    private HkXhSpecRuleMapper specMapper;

    @Autowired
    private HospitalDetailTmpMapper hospitalDetailTmpMapper;

    @Autowired
    private DrugEnterRecordMapper drugEnterRecordMapper;

    @Autowired
    private DataCatalogMapper dataCatalogMapper;

    @Autowired
    private OutHospitalInfoMapper outHospitalInfoMapper;

    @Autowired
    private GfjsZzdMapper zzdMapper;

    @PostConstruct
    @Override
    public int queryAllMx() {
        List<Map<String, String>> ypList = commonMapper.queryCommonRule();
        List<Map<String, String>> zlList = specMapper.querySpecRule();
        List<Map<String, String>> catalogList = dataCatalogMapper.queryAllCatalog();
        ypList.addAll(zlList);
//        String[] strings = new String[]{
//                "1",
//                "3",
//                "4",
//                "11897",
//                "12215",
//                "12230",
//                "12350"
//        };
//        Constant.setSpecialNo(Arrays.asList(strings));
        String[] ypcds = new String[]{
                "2",
                "4",
                "6",
                "9",
                "11",
                "13",
                "16"
        };
        Constant.setYpcds(Arrays.asList(ypcds));
        Map<String, String> mapDataCatalog = new HashMap<>();
        for (Map<String, String> item : catalogList) {
            mapDataCatalog.put(String.valueOf(item.get("id")), item.get("name"));
        }

        Constant.setDataCatalogMap(mapDataCatalog);
        Map<String, String> mapData = new HashMap<String, String>();
        for (Map<String, String> map : ypList) {
            mapData.put(map.get("key"), map.get("name"));
        }
        Constant.setMxMap(mapData);


        //加入 住院分娩 鍵值對

        Map<String, String> operaMap = new HashMap<>();
        operaMap.put("23768-zl", Constant.DiganosCode.SHUN_CHAN + "");
        operaMap.put("23773-zl", Constant.DiganosCode.SHUN_CHAN + "");
        operaMap.put("23778-zl", Constant.DiganosCode.POU_FU_CHAN + "");
        operaMap.put("23779-zl", Constant.DiganosCode.POU_FU_CHAN + "");
        operaMap.put("23780-zl", Constant.DiganosCode.POU_FU_CHAN + "");
        operaMap.put("23781-zl", Constant.DiganosCode.POU_FU_CHAN + "");
        Constant.setOperationMap(operaMap);
        System.out.println("药数据总数为：" + Constant.getMxMap().size());
        return Constant.getMxMap().size();
    }

    @Override
    public int queryNoRuleCount() {
        return hospitalDetailTmpMapper.queryCount();
    }

    @Override
    public List<String> queryLocalNo() {

        List<String> commonList = commonMapper.queryAllNo();
        List<String> specList = specMapper.queryAllNo();
        List<String> tmpList = hospitalDetailTmpMapper.queryAllNo();
        commonList.addAll(specList);
        commonList.addAll(tmpList);
        return commonList;
    }

    @Override
    public int uploadData(List<BaseDataEntity> request) {
        for (BaseDataEntity item : request) {
            double d2 = Double.valueOf(item.getFYDJ());
            float f2 = (float) (d2 * 100);
            String s2 = String.valueOf(f2);
            int fee = Integer.valueOf(s2.split("\\.")[0]);
            item.setFYDJ(fee + "");
        }
        return hospitalDetailTmpMapper.insertByList(request);
    }

    @Override
    public int queryYpcdCount() {
        return drugEnterRecordMapper.queryCount();
    }

    @Override
    public List<String> queryYpcdNo() {
        return drugEnterRecordMapper.queryAllNo();
    }

    @Override
    public int uploadYpcds(List<YpcdEntity> list) {
        return drugEnterRecordMapper.insertByList(list);
    }

    @Override
    public List<String> queryNeedUploadUser() {
        return drugEnterRecordMapper.queryUserData();
    }

    @Override
    public int updateUserFlag(String zyh) {
        return drugEnterRecordMapper.updateUserFlag(zyh);
    }

    @Override
    public int insertCyxj(CyxjInfoEntity entity) {
        OutHospitalInfo info = new OutHospitalInfo();
        info.setZzdh(entity.getZzdh());
        info.setOutInfo(entity.getInfo());
        int result = outHospitalInfoMapper.insertInfo(info);
        if (result > 0) {
            result = zzdMapper.updateSummaryFlag(entity.getZzdh());
        }
        return result;
    }
}
